package com.snailct.bpp.router.datachange.service;

import com.snailct.bpp.router.datachange.DaoOperation;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by huangxa on 2016/6/17.
 */
public class AcceptMessage {
    private String xmlStr;

    public String getXmlStr() {
        return xmlStr;
    }

    public void setXmlStr(String xmlStr) {
        this.xmlStr = xmlStr;
    }
    //接收报文
    public void insertXMLStr() {
        String sqlStr = "insert into SOA_SYS_ACCEPT_MESSAGE(XMLSTR) value('"+xmlStr+"')";

        DaoOperation.execute(sqlStr);

        String queryIdStr = "select max(AUTO_ID) maxId from SOA_SYS_ACCEPT_MESSAGE";
        ResultSet rs = DaoOperation.executeQuery(queryIdStr);
        try {
            String refId=null;
            if(rs.next()){
                refId = rs.getString("maxId");
            }

            String selectQuene = "select * from SOA_SYS_TASK_QUEUE where TASKDESC='acceptMessage' and refId='"+refId+"'";
            rs = DaoOperation.executeQuery(selectQuene);
            if(!rs.next()){
                String insertMessage = "insert into SOA_SYS_TASK_QUEUE(taskDesc,status,refId) values('acceptMessage','0','"+refId+"')";

                DaoOperation.execute(insertMessage);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //接收报文
    public String insertXMLStrSyn() {
        String sqlStr = "insert into SOA_SYS_ACCEPT_MESSAGE(XMLSTR) value('"+xmlStr+"')";

        DaoOperation.execute(sqlStr);

        String queryIdStr = "select max(AUTO_ID) maxId from SOA_SYS_ACCEPT_MESSAGE";
        ResultSet rs = DaoOperation.executeQuery(queryIdStr);
        String refId=null;
        try {

            if(rs.next()){
                refId = rs.getString("maxId");
            }

            String selectQuene = "select * from SOA_SYS_TASK_QUEUE where TASKDESC='acceptMessage' and refId='"+refId+"'";
            rs = DaoOperation.executeQuery(selectQuene);
            if(!rs.next()){
                String insertMessage = "insert into SOA_SYS_TASK_QUEUE(taskDesc,status,refId) values('acceptMessage','1','"+refId+"')";

                DaoOperation.execute(insertMessage);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return refId;
    }
}
